package com.yunhe.policyfulfillment.policyful.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.model.mybatis.PageResult;
import com.yunhe.policyfulfillment.constant.PolicyFulfillmentConstant;
import com.yunhe.policyfulfillment.policyful.domain.Banner;
import com.yunhe.policyfulfillment.policyful.domain.PolicyInfoNews;
import com.yunhe.policyfulfillment.policyful.model.BannerQueryCondition;
import com.yunhe.policyfulfillment.policyful.repository.BannerRepository;
import com.yunhe.policyfulfillment.policyful.service.BannerService;
import com.yunhe.policyfulfillment.util.MyHeaderUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName: BannerServiceImpl
 * @Description:
 * @author: GongDaCheng
 * @Date: 15:44 2021/7/21
 */
@Service
public class BannerServiceImpl implements BannerService {

    @Autowired
    MyHeaderUtil myHeaderUtil;

    @Autowired
    BannerRepository bannerRepository;

    @Override
    public int create(Banner banner) {
        banner.setPublishPerson(myHeaderUtil.getHeaderUserId());
        long time = banner.getPublishTime().getTime();
        if(time <= System.currentTimeMillis()){
            banner.setOnline(PolicyFulfillmentConstant.Online_Type.Online.value());
        }else {
            banner.setOnline(PolicyFulfillmentConstant.Online_Type.Offline.value());
        }
        banner.setTop(PolicyFulfillmentConstant.Top_Type.NotTop.value());
        banner.setUpdateTime(new Date());
        banner.setCreateTime(new Date());
        int count = bannerRepository.create(banner);
        return count;
    }

    @Override
    public Banner getById(Long id) {
        Banner byId = bannerRepository.getById(id);
        if (byId != null) {
            byId.setPublishPersonName(myHeaderUtil.getUserName(byId.getPublishPerson()));
        }
        if (byId.getUpdatePerson() != null){
            byId.setUpdatePersonName(myHeaderUtil.getUserName(byId.getUpdatePerson()));
        }
        return byId;
    }

    @Override
    public int update(Banner banner) {
        long time = banner.getPublishTime().getTime();
        if (time <= System.currentTimeMillis()){
            banner.setOnline(PolicyFulfillmentConstant.Online_Type.Online.value());
        }else {
            banner.setOnline(PolicyFulfillmentConstant.Online_Type.Offline.value());
        }
        if (myHeaderUtil.getHeaderUser() != null) {
            banner.setUpdatePerson(myHeaderUtil.getHeaderUserId());
        }
        int count = bannerRepository.update(banner);
        return count;
    }

    @Override
    public PageResult getByCondition(BannerQueryCondition bannerQueryCondition) throws ArgumentErrorException {
        PageResult result = null;
        if (bannerQueryCondition.getPublishPerson()==null) {
            Long userId = myHeaderUtil.getHeaderUserId();
            if(userId == null){
                throw new ArgumentErrorException("登录信息已失效,请重新登录");
            }
            //bannerQueryCondition.setPublishPerson(userId);
        }
        PageHelper.startPage(bannerQueryCondition.getPage(),bannerQueryCondition.getSize());
        List<Banner> banners = bannerRepository.getByCondition(bannerQueryCondition);
        for (Banner banner : banners) {
            banner.setPublishPersonName(myHeaderUtil.getUserName(banner.getPublishPerson()));
            if (banner.getUpdatePerson() != null){
                banner.setUpdatePersonName(myHeaderUtil.getUserName(banner.getUpdatePerson()));
            }
        }
        PageInfo<Banner> pageInfo = new PageInfo<>(banners);
        result = new PageResult(bannerQueryCondition.getPage(),bannerQueryCondition.getSize(), (int) pageInfo.getTotal(),banners);
        return result;
    }

    @Override
    public void onlineOffline(String ids, String targetStatus) {
        List<String> idList = Arrays.stream(ids.split(",")).collect(Collectors.toList());
        for (String idStr : idList) {
            Long id = Long.valueOf(idStr);
            Banner byId = bannerRepository.getById(id);
            if (myHeaderUtil.getHeaderUser() != null){
                byId.setUpdatePerson(myHeaderUtil.getHeaderUserId());
            }
            bannerRepository.update(byId);
            if (PolicyFulfillmentConstant.Online_Type.Online.value().equals(targetStatus)) {
                //上线时更新发布时间
                bannerRepository.online(id, targetStatus, new Date());
            }else {
                bannerRepository.setBannerStatus(id, targetStatus);
            }
        }
    }

    @Override
    public void top(Long id) {
        Banner byId = bannerRepository.getById(id);
        if (myHeaderUtil.getHeaderUser() != null){
            byId.setUpdatePerson(myHeaderUtil.getHeaderUserId());
        }
        bannerRepository.update(byId);
        bannerRepository.top(id, PolicyFulfillmentConstant.Top_Type.Top.value(), PolicyFulfillmentConstant.Online_Type.Online.value(), new Date());
    }

    @Override
    public int deleteById(Long id) {
        int count = bannerRepository.delete(id);
        return count;
    }

    @Override
    public List<Banner> getBanner() {
        List<Banner> banners = bannerRepository.getBanner();
        for (Banner banner : banners) {
            if (banner.getUpdatePerson() != null){
                banner.setUpdatePersonName(myHeaderUtil.getUserName(banner.getUpdatePerson()));
            }
        }
        return banners;
    }

    @Override
    public void publishTimedBanner() {
        bannerRepository.publishTimedBanner();
    }


}
